﻿
#region Namespaces
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WebReference;
using Session;
#endregion


public partial class Admin_CalorificAnal : System.Web.UI.Page
{
    Service objService = new Service();

    #region UI Events

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
        }
        else
        {
            string ExperimentId = (string)Session["ExperimentId"];

            if (ExperimentId != null)
            {
                lblExpdetails.Text = "Your Experiment Id is - " + ExperimentId;
            }

            BindDataGrid();

            string id = Request.QueryString["id"];

            if (id != null)
            {
                fillCalorificAnalysisDetails(Convert.ToInt32(id));
                txtCalAnalId.Text = string.Empty;
                trCalorificAnalGrid.Visible = false;
                trCalorifictextboxes.Visible = true;
            }
        }
    }
    protected void btnSave_Click1(object sender, ImageClickEventArgs e)
    {
        string RefeId = (string)Session["ReferenceId"];

        if (txtCalAnalId.Text != string.Empty)
        {
            lblMsg.Text = objService.UpdateCalAnal(ddlMoistureStatus.SelectedValue.ToString(), txtHhv.Text, txtLhv.Text, Convert.ToInt32(txtCalAnalId.Text));
            BindDataGrid();
            clearTextboxes();
        }
        else
        {
            lblMsg.Text = objService.SaveCalAnal(ddlMoistureStatus.SelectedValue.ToString(), txtHhv.Text, txtLhv.Text);

            string CalAnalID = objService.GetLastInsertedId("ref_fk", RefeId, "calanal");
               
            objService.UpdateTable("calanal", "ref_fk", RefeId, CalAnalID);

            clearTextboxes();

            if ((string)Session["ExperimentId"] != null)
            {
                string ExpId = (string)Session["ExperimentId"];
                if (objService.UpdateExperiment("calanal_fk", CalAnalID, ExpId) != "Experiment Successfully Updated")
                {
                    objService.DeleteRecord("calanal", Convert.ToInt32(CalAnalID));
                    lblMsg.Text = "Admin - You can't enter the multiple CalAnalysis record for one experiment";
                    BindDataGrid();
                }
                else
                {
                    lblMsg.Text = "Calorific Analysis Saved and " + objService.UpdateExperiment("calanal_fk", CalAnalID, ExpId);
                    BindDataGrid();
                }

                lblExpdetails.Text = "Your Experiment Id is - " + ExpId;
            }
            else
            {
                lblMsg.Text = "Calorific Analysis Saved and " + objService.SaveExperiment(null, null, CalAnalID, null, null, null, null, null, null, null, null, RefeId, null);
                string ExperimentId = objService.GetExperimentId("calanal_fk", CalAnalID);
                Session["ExperimentId"] = ExperimentId;

                lblExpdetails.Text = "Your Experiment Id is - " + ExperimentId;

                BindDataGrid();
            }
        }
    }

    protected void dgCalorificAnalysis_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        dgCalorificAnalysis.PageIndex = e.NewPageIndex;
        BindDataGrid();

    }

    protected void dgCalorificAnalysis_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int id = Convert.ToInt32(dgCalorificAnalysis.Rows[e.NewEditIndex].Cells[1].Text.Trim());
        fillCalorificAnalysisDetails(id);
        trCalorificAnalGrid.Visible = false;
        trCalorifictextboxes.Visible = true;
        btnPreviousRecord.Visible = false;

    }

    #endregion


    #region Private method

    private void BindDataGrid()
    {
        string ExpId = (string)Session["ExperimentId"];
        if (ExpId == null)
        {
            lblMsg.Text = "You haven't entered any data for the Calorific analysis to this experiment";
        }
        else
        {
            Int32 CalAnalId = objService.GetExperimentFKValues("calanal_fk", ExpId);
            if (CalAnalId.ToString() == null)
            {
                lblMsg.Text = "You haven't entered any data for the Calorific analysis to this experiment";
            }
            else
            {
                if (objService.GetCalorificAnalysis(CalAnalId.ToString()).Tables[0].Rows.Count < 1)
                {
                    trCalorifictextboxes.Visible = true;
                    trCalorificAnalGrid.Visible = false;
                }
                else
                {
                    dgCalorificAnalysis.DataSource = objService.GetCalorificAnalysis(CalAnalId.ToString());
                    dgCalorificAnalysis.DataBind();
                    trCalorifictextboxes.Visible = false;
                    trCalorificAnalGrid.Visible = true;

                }
                
               
            }
        }

        

    }
      
    private void fillCalorificAnalysisDetails(Int32 Id)
    {

        DataSet ds = new DataSet();
        ds = objService.GetTableDetailsBasedonPK("calanal", Id);

        txtCalAnalId.Text = ds.Tables[0].Rows[0]["id"].ToString();
        txtHhv.Text = ds.Tables[0].Rows[0]["Hhv"].ToString();
        txtLhv.Text = ds.Tables[0].Rows[0]["Lhv"].ToString();
        ddlMoistureStatus.DataSource = ds.Tables[0];
        ddlMoistureStatus.DataValueField   = ds.Tables[0].Rows[0]["moistureStatus"].ToString();
        //ddlMoistureStatus.DataBind;

    }
    
    private void clearTextboxes()
    {
        txtHhv.Text = string.Empty;
        txtLhv.Text = string.Empty;
        txtCalAnalId.Text = string.Empty;
    }

    #endregion   


    protected void btnPreviousRecord_Click(object sender, EventArgs e)
    {
        Response.Redirect("Previous Pages/PreCalorificAnal.aspx");
    }
}
